<template>
  <div>

    <el-card style="margin-top: 10px;">

     <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理</el-breadcrumb-item>
      <el-breadcrumb-item>成员管理</el-breadcrumb-item> -->
      <div class="manager-all">
          <div class="project-manager"></div>
          <div class="project-item">
            <el-breadcrumb-item :to="{ path: '/' }"
            >组管理
            </el-breadcrumb-item
            >
          </div>
        </div>
    </el-breadcrumb>

      <el-row style="margin-top:-60px;">
        <!-- <el-col :span="5" style="margin-left:-100px;">
          <el-button type="primary" @click="openAddDialog" class="add"
          ><img src="../../icon标/添加数据.png" class="addimg" alt=""/>
            添加新成员
          </el-button>
        </el-col> -->
        <el-col :span="16"></el-col>
        <el-col :span="3" class="row-flex" style="float: right;">
          <el-button type="primary" @click="goToLinkByUser" class="add"
          >返回用户列表
          </el-button>
        </el-col>
        <el-col :span="3" class="row-flex" style="float: right;">
          <el-button type="primary" @click="goToLinkByGroup" class="add"
          >返回组列表
          </el-button>
        </el-col>
      </el-row>

        <hr
        style="width: 1642px;
                height: 1px;
                border:none;
                background: #E8ECF0;
                margin-left:-23px;
                margin-top:5px;"
      />

    <el-col :span="5" style="margin-top: 20px;margin-left: 50px;">
          <el-button type="primary" @click="openAddDialog" class="add el-icon-plus"
          ><!-- <img src="../../icon标/添加数据.png" class="addimg" alt=""/> -->
            添加新成员
          </el-button>
        </el-col>

      <div style="margin-top: 25px;">组名：
        <el-input type="text" v-model="groupName" readonly=""
                  style="width: 200px;"></el-input></div>
      <!--      //需要 用户名 和他在的所有组-->
      <!--      展示形式为列表有点难看吧-->
      <el-table
        :data="memberList"
        style="width:90%;margin-left: 50px;margin-top:20px;"
        class="bioTable"
        :header-cell-style="{ background: '#F6F7FB' }">
        <el-table-column label="成员名称" prop="username"></el-table-column>
        <el-table-column
          label="权限"
          prop="permission">
          <template slot-scope="scope">
            <el-select v-model="scope.row.permission" placeholder="请选择" @change="changePower(scope.row)">
              <el-option label="只读" :value="1"></el-option>
              <el-option label="读写" :value="0"></el-option>
              <el-option label="无权限" :value="-1"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          label="管理"
          prop="level">
          <template slot-scope="scope">
            <div class="btn-all">
              <!--              <div class="bj">-->
              <!--                <i class="el-icon-edit-outline" style="color:#409EFF;"></i>-->
              <!--                &lt;!&ndash;              整个Button&ndash;&gt;-->
              <!--                <router-link-->
              <!--                  type="success"-->
              <!--                  :underline="false"-->
              <!--                  style="margin-left:5px;font-size:14px;cursor: pointer;"-->
              <!--                  :to="'/groupMember/' + scope.row.groupId"-->
              <!--                >进入该组-->
              <!--                </router-link>-->
              <!--              </div>-->
              <!--              <div class="sx"></div>-->
              <div class="sc">
                <i class="el-icon-delete" style="color:#F56C6C;"></i>
                <el-link
                  type="danger"
                  :underline="false"
                  @click="removeUserInGroup(scope.row)"
                  style="margin-left:5px;font-size:14px;cursor: pointer;"
                >请出该组
                </el-link>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :visible.sync="addFormVisible"
      title="添加新成员"
      width="800px"
      @close="addDialogClosed"
    >
    <div class="underline"></div>
    <div class="under"></div>
<!--            <el-form-->
<!--              :model="addForm"-->
<!--              ref="addUserRef"-->
<!--              label-width="140px"-->
<!--              class="molecular-design"-->
<!--              :rules="addMemberRules"-->
<!--            >            </el-form>-->
              <el-row style="margin-top:30px;margin-left: 60px;">
                <el-transfer v-model="selectUser" :data="allUser"
                             :props="{key: 'id',label: 'username'}"
                             :titles="['待选成员', '已有成员']"
                             :button-texts="['移除', '添加']"
                             filterable
                ></el-transfer>
                <!--          <el-transfer-->
                <!--            filterable-->
                <!--            :filter-method="filterMethod"-->
                <!--            filter-placeholder="请输入城市拼音"-->
                <!--            v-model="this.allUser.username"-->
                <!--            :data="this.allUser">-->
                <!--          </el-transfer>-->
              </el-row>
      <!--      底部区域-->
      <span slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false" plain>取 消</el-button>
          <el-button
            type="primary"
            @click="addUser"
            style="margin-left:50px;margin-bottom:15px;"
          >确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";

export default {
  name: "GroupMember",
  data() {
    return {
      groupName:'',
      memberList: [],
      addFormVisible: false,
      addForm: [],
      queryInfo: {
        // query: "",
        // 当前的页数
        page: 1,
        // 当前每页显示多少条数据
        size: 99999,
      },

      // deleteList:[],
      query: {
        id: 0
      },
      addMemberRules: {},
      allUser: [],
      selectUser: [],

    }
  },
  created() {
    //同时可能拥有U和G 优先级？ 目前是G
    var query=this.$route.query
    var Upage=query.Upage
    var Usize=query.Usize
    var Gpage=query.Gpage
    var Gsize=query.Gsize
    this.query.id = this.$route.params.id;
    this.selectGroupName()
    this.getMemberList()

  },
  methods: {
    async selectGroupName() {
      // const {data: res} = await this.$axios.post(`group/list?page=${this.queryInfo.page}&size=${this.queryInfo.size}&query=${this.queryInfo.query}`);
      const {data: res} = await this.$axios.get("group/getName", {
        params: {
          id: this.$route.params.id
        }
      });
      console.log('.1')
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error("获取角色信息失败！");
      }
      this.groupName = res.data
    },
    // 这个不搞分页
    async getMemberList() {
      const {data: res} = await this.$axios.get("group/getGroupUser", {
        params: this.query
      });
      if (res.code !== 200) {
        return this.$message.error("获取角色列表失败！");
      }
      this.memberList = res.data;
      this.total = res.total;
      console.log('now')
      console.log(this.memberList);

    },
    goToLinkByUser() {
      //指定跳转地址
      // this.$router.replace('/powerManaged')
      if(this.$route.query.Uid==null){
        return this.$router.push({path:`/userManaged/`})
      }
      this.$router.push({path:`/powerManaged/${this.$route.query.Uid}`,query:{Upage:this.$route.query.Upage,Usize:this.$route.query.Usize}})

    },
    goToLinkByGroup() {
      //指定跳转地址
      // this.$router.replace('/groupManaged')
      this.$router.push({path:'/groupManaged',query:{Gpage:this.$route.query.Gpage,Gsize:this.$route.query.Gsize}})
    },
    async removeUserInGroup(row) {
      console.log(row)
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作该用户将退出本小组, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
          // 如果取消 会抛出一个错误error 这里用catch捕获他 然后返回error
          // 可以简写成.catch(err => err)
        }
      ).catch(error => {
        return error;
      });
      // 如果用户确认删除，返回值为字符串 confirm
      // 如果用户取消删除，返回值为字符串 cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("以取消删除");
      }
      const {data: res} = await this.$axios.post(`group/unAccredit?groupId=${row.groupId}&userId=${row.userId}`);
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("请出失败！");
      }
      this.$message.success("请出成功!");
      await this.getMemberList();

    },
    async changePower(row) {
      const {data: res} = await this.$axios.post("group/setGroupAuth", row);
      if (res.code !== 200) {
        return this.$message.error("获取角色列表失败！");
      }
      console.log(res)
      this.memberList = res.data;
      console.log('now')
      await this.getMemberList()


    },
    async addUser() {
      for (const item of this.selectUser) {
        console.log(item)
        const {data: res} = await this.$axios.post(`group/accredit?groupId=${this.$route.params.id}&userId=${item}`);
        console.log(res)
        if (res.code !== 200&&res.msg!=='成员已经存在') {
          return this.$message.error("表单提交失败！");
        }
      }
      this.$message.success("表单提交成功！");
      // 隐藏添加用户的对话框
      this.addFormVisible = false;
      // 重新获取用户的列表
      await this.getMemberList();
    },
    async openAddDialog() {

      const {data: res} = await this.$axios.get("userManager/getUser", {
        params: this.queryInfo
      });
      this.selectUser=[]
      //key:id label:username
      //设置左框（全部）
      this.allUser=res.data
      //allUser是所有人
      //memberList是已经在组里的成员
      //selectUser是右边的数据

      // 目标是对allUser中有memberList的加上disabled
      // 还有一个办法是 copy一个新的 allUser 一行一行往里加 加的时候对应memberList 如果Id有对应 顺便加上disabled
      this.memberList.forEach(item => {
        //向selectUser加入如数据
        this.selectUser.push(item.userId);
        //item有groupId和userId
        console.log('item')
        console.log(item)
        // let newItem={
        //   groupId:item.groupId,
        //   premission:item.premission,
        //   userId:item.userId,
        //   disabled:true
        // }
        // console.log('newItem')
        // console.log(newItem)
      })
      console.log('allU')
      console.log(this.allUser)
      this.addFormVisible = true
    },
    addDialogClosed() {
      this.getMemberList()
      // this.$refs.addUserRef.resetFields();
    },
  }
}
</script>

<style>
.bioTable {
  margin-top: 20px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  /*width: 90px;*/
  color: #99a9bf;
}

.demo-table-expand {
  margin-right: 0;
  margin-bottom: 0;
}

.expand-msg {
  border-radius: 1px !important;
  border: 1px solid rgba(239, 239, 239, 0.36);
  /*height: 50px;*/
  line-height: 50px;
  /*border: 1px solid #000 !important;*/
  /*border-right: none;*/
}

.expand-msg-left {
  border-left: 2px solid rgba(239, 239, 239, 0.36);
  /*border-left: 2px solid #d7dae2;*/
}

.table-item {
  margin-bottom: 0px !important;
  line-height: 50px;
  padding-left: 10px;
}

.table-item-span {
  line-height: 50px;
}

.add-button {
  margin-top: 5px;
}

.table-image {
  width: auto;
  height: auto;
  /*max-width: 50%;*/
  /*max-height: 50%;*/
  display: block;
}

/*.el-form-item .table-item label{*/
/*  line-height: 50px;*/
/*  border-left: 2px solid #d7dae2;*/
/*  padding-left: 10px;*/
/*}*/
/*.el-form-item .demo-table-expand{*/
/*  margin-bottom: 0px !important;*/
/*}*/

/*.el-form-item__content{*/
/*  line-height: 50px;*/
/*  border-left: 2px solid #d7dae2;*/
/*  padding-left: 10px;*/
/*}*/
/*.el-form-item__label{*/
/*  padding-left: 10px;*/
/*}*/
.btn-all {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.cell {
  text-align: center;
}

.sx {
  width: 1.5px;
  height: 18px;
  /* font-size: 14px;
  font-weight: 400; */
  background: #ccccccff;
}

.addimg {
  width: 22px;
  height: 16px;
  margin-left: 0px;
  margin-top: 0;
  float: left;
}

a {
  text-decoration: none;
  color: #67c23a;
}

.Biosearch {
  margin-top: 25px;
  background: rgba(247, 247, 247, 1);
  display: flex;
  height: 100px;
  align-items: center;
  padding: 0 30px;
  justify-content: space-between;
}
.underline {
  width: 100px;
  height: 3px;
  background: #4087fc;
  margin-top: -34px;
  float: left;
}
.under{
  width: 635px;
  height: 2px;
  background: #cbdcf8 !important;
  float: right;
  margin-right: 25px;
  margin-top: -33px;
}
</style>
